package controller 
{
	import display.DisplayControl;
	import events.ViewEvent;
	import flash.events.EventDispatcher;
	import model.Player;
	import org.osmf.events.MediaPlayerStateChangeEvent;
	import events.ControllerEvent;
	
	
	/**
	 * ...
	 * @author Henrik Riise Hansen
	 */
	public class Controller extends EventDispatcher 
	{
		private var shell:Shell;
		
		// view
		
		private var videoControl:DisplayControl;
		
		// player
		
		private var player:Player;
		
		public function Controller(_shell:Shell) 
		{
			_shell = _shell;
			
		}
		
		public function start(_videoControl:DisplayControl,_player:Player):void
		{
			videoControl = _videoControl;
			videoControl.addEventListener(ViewEvent.PLAY, onPlay);
			videoControl.addEventListener(ViewEvent.PAUSE, onPause);
			videoControl.addEventListener(ViewEvent.SEEK, onSeek);
			
			player = _player;
			player.addEventListener(ControllerEvent.STATE, onMediaPlayerStateChange);
			player.addEventListener(ControllerEvent.PROGRESS, onMediaPlayerProgress);
		}
		
		private function onMediaPlayerProgress(e:ControllerEvent):void 
		{
			dispatchEvent(new ControllerEvent(ControllerEvent.PROGRESS, { time:e.data.time, duration:e.data.duration } ));
		}
		
		private function onMediaPlayerStateChange(e:ControllerEvent):void
		{
			dispatchEvent(new ControllerEvent(ControllerEvent.STATE, { state:e.data.state } ));
		}
		
		private function onPlay(e:ViewEvent):void
		{
			dispatchEvent(new ViewEvent(ViewEvent.PLAY));
		}
		
		private function onPause(e:ViewEvent):void
		{
			dispatchEvent(new ViewEvent(ViewEvent.PAUSE));
		}
		
		private function onSeek(e:ViewEvent):void
		{
			dispatchEvent(new ViewEvent(ViewEvent.SEEK, { seek:e.data.seek } ));
		}
		
	}

}